<?php
/**
 * Description of YuiDemoController
 *
 * @author Serg Kosiy
 */
class widgetController extends CController
{

    public function actionIndex()           // Start page, full widgets list
    {
        $this->render('list/index');
    }


    public function actionSimple()          // List of simple widgets
    {
        $this->render('list/simple');
    }
    public function actionActive()          // List of Active Widgets
    {
        $this->render('list/active');
    }

    public function actionAjaxcall()        // Echo ajax request from widgets
    {
       echo '<br>Request from <b>',$_POST['id'], '</b>, value = <b>', $_POST['value'],'</b>';
    }

    public function actionInit()            // List of simple widgets
    {
        $this->render('simple/init');
    }
    public function actionAutocomplete()    // Autocomplete
    {
        if(Yii::app()->request->isAjaxRequest) // return data array for autocomplete request
        {
            if(isset($_GET['query']))
                $data = '{"Result":'.json_encode(array(array('name'=>'Asia'),
                                                       array('name'=>'Africa'),
                                                       array('name'=>'and Europe'))).'}';
            else
                $data = '';
            $this->renderPartial('ajaxView', $data);
        }
        else    // no ajax request
        {
            $ac = (isset($_POST['acTable'])) ? $_POST['acTable'] : '';
            $data = array('id'=>'acTable',
                          'value'=>$ac,
                          'tableName'=>'autocompleteDemo',
                          'fieldName'=>'name');
            $this->render('simple/autocomplete', $data);
        }
    }
    public function actionActiveac()        //Active Autocomplete
    {
        if(Yii::app()->request->isAjaxRequest) // return data array for autocomplete request
        {
            if(isset($_GET['query']))
                $data = '{"Result":'.json_encode(array(array('name'=>'Asia'),
                                                       array('name'=>'Africa'),
                                                       array('name'=>'and Europe'))).'}';
            else
                $data = '';
            $this->renderPartial('ajaxView', $data);
        }
        else    // no ajax request
        {
            $rec = tableDemo::model()->findByPk(1);
            if(isset($_POST['acTable']))
            {
                $rec->charField = $_POST['acTable'];
                $rec->save();
            }
            $data = array('id'=>'acTable',
                          'value'=>$rec->charField,
                          'tableName'=>'autocompleteDemo',
                          'fieldName'=>'name');
            $this->render('active/autocomplete', $data);
        }
    }
    public function actionCalendar()        // Calendar
    {
        $date1 = (isset($_POST['date1'])) ? $_POST['date1'] : null;
        $date2 = (isset($_POST['date2'])) ? $_POST['date2'] : null;
        $date3 = (isset($_POST['date3'])) ? $_POST['date3'] : null;
        $data = array('value1'=>$date1,
                      'value2'=>$date2,
                      'value3'=>$date3);
        $this->render('simple/calendar', $data);
    }
    public function actionCalendarPartial() // Partial view Calendar
    {
        $date1 = (isset($_POST['date1'])) ? $_POST['date1'] : null;
        $date2 = (isset($_POST['date2'])) ? $_POST['date2'] : null;
        $date3 = (isset($_POST['date3'])) ? $_POST['date3'] : null;
        $data = array('value1'=>$date1,
                      'value2'=>$date2,
                      'value3'=>$date3);
        $this->renderPartial('simple/calendar', $data);
    }
    public function actionActivecalendar()  // Calendar
    {
        $calendar = calendarDemo::model()->findByPk(1);
        if(isset($_POST['calendarDemo']))
            {
                $calendar->attributes = $_POST['calendarDemo'];
                $calendar->save();
            }
        $this->render('active/calendar', array('calendarDemo'=>$calendar));
    }
    public function actionTabview()         // TabView
    {
        $activeTab1 = (isset($_POST['tab1'])) ? $_POST['tab1'] : 0;
        $tabs1 = array(
            CYui::tabviewContentTab('tab content here ...', 'Simple text'),
            CYui::tabviewRedirectTab('/widget/active', 'Redirect page'),
            CYui::tabviewAjaxTab('/widget/calendarpartial', 'Calendar view', true),
        );

        $activeTab2 = (isset($_POST['tab2'])) ? $_POST['tab2'] : 0;
        $tabs2 = array(
            CYui::tabviewContentTab('The TabView component is designed to enable developers to create navigable tabbed views of content.', 'First tab'),
            CYui::tabviewContentTab('In our experience, the YUI TabView Control works well on A-Grade-based mobile devices.', 'Second tab'),
            CYui::tabviewContentTab('The YUI Library is a set of utilities and controls, written in JavaScript, for building richly interactive web applications using techniques such as DOM scripting, DHTML and AJAX. YUI is available under a BSD license and is free for all uses. .', 'YUI'),
            CYui::tabviewContentTab('The TabView component is designed to enable developers to create navigable tabbed views of content.', '12345'),
        );
        $this->render('simple/tabview', array('id1'=>'tab1', 'tabs1'=>$tabs1, 'activeTab1'=>$activeTab1, 'orientation1'=>'top',
                                              'id2'=>'tab2', 'tabs2'=>$tabs2, 'activeTab2'=>$activeTab2, 'orientation2'=>'bottom'));
    }
    public function actionTree()            // TreeView
    {
        $value1 = (isset($_POST['tree1'])) ? $_POST['tree1'] : 1;
        $value2 = (isset($_POST['tree2'])) ? $_POST['tree2'] : 1;
        $value3 = (isset($_POST['tree3'])) ? $_POST['tree3'] : 1;
        $data = array('name1'=>'tree1', 'value1'=>$value1, 'tableTree1'=>'treeDemo', 'editable1'=>false, 'asyncMode1'=>false,
                      'name2'=>'tree2', 'value2'=>$value2, 'tableTree2'=>'treeDemo', 'editable2'=>false, 'asyncMode2'=>true,
                      'name3'=>'tree3', 'value3'=>$value3, 'tableTree3'=>'treeDemo', 'editable3'=>true, 'asyncMode3'=>true,
                     );
        $this->render('simple/tree', $data);
    }
    public function actionActivetree()      // ActiveTreeView
    {
        $tableDemo = tableDemo::model()->findByPk(1);
        if(isset($_POST['tableDemo']))
            {
                $tableDemo->attributes = $_POST['tableDemo'];
                $tableDemo->save();
            }
        $this->render('active/tree', array('tableDemo'=>$tableDemo));
    }
    public function actionTable()           //DataTable
    {
        $columns = CYuiTable::getAllColumnsFromTable('tableDemo');
        $p = CYuiTable::paginatorCreate(5);
        CYuiTable::paginatorSetTemplate($p, '<p>{CurrentPageReport}{FirstPageLink} {PreviousPageLink} {NextPageLink} {LastPageLink}
    <label> Page size: {RowsPerPageDropdown}</label>  {PageLinks} </p>');

        CYuiTable::paginatorRowsPerPageDropdown($p, array( array('value'=>5, 'text' => "5"),
                                                           array('value'=>10, 'text' => "10"),
                                                           array('value'=>15, 'text' => "15")
                                                       )
                                                );
        CYuiTable::columnHide($columns, 'id');
        $table = CYuiTable::tableCreate('tableDemo');
        CYuiTable::tableSetPaginator($table, $p);
        CYuiTable::tableSetSize($table, '40em');
        CYuiTable::columnSetAttribute(&$columns, 'intField', 'readOnly', true);
        CYuiTable::columnSetAttribute(&$columns, 'intField', 'label', 'Read only');
        CYuiTable::columnSetLookup(&$columns, 'lookupField', 'autocompleteDemo', 'id', 'name');
        CYuiTable::tableSetColumns($table, $columns);
        $this->render('simple/table', array('table'=>$table));
    }
    public function actionEditor()
    {
        $value1 = (isset($_POST['editor1'])) ? $_POST['editor1'] : 'Editor1 context here...';
        $value2 = (isset($_POST['editor2'])) ? $_POST['editor2'] : 'Editor2 context here...';
        $this->render('simple/editor', array('text1'=>$value1, 'text2'=>$value2));
    }
    public function actionLightbox()
    {
        $this->render('simple/lightbox');
    }
    public function actionLayout()
    {
        $data = array(
            CYui::layoutUnitCenter('centerDiv'),
            CYui::layoutUnitLeftRight('left', 'leftDiv', 200, true, '10px', 'header'),
            CYui::layoutUnitTopBottom('top', 'topDiv', 150)
        );
        $this->render('simple/layout', array('units'=>$data));
    }
}
?>
